import pandas as pd
import pymysql

from flask import Flask, render_template, request

app = Flask(__name__)

db = pymysql.connect(host="localhost", user="root", password="123456", database="project_01")
cursor = db.cursor()


# sql = 'select * from side_table where type = "点位"'

def select_all_mysql(sql):
    db = pymysql.connect(host="localhost", user="root", password="123456", database="project_01")
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        # 只能有一个cursor.fetchall()
        result = cursor.fetchall()
        return result
    except:
        import traceback
        traceback.print_exc()
        print("Error: unable to fetch data")
    db.close()


@app.route('/')
def index():
    return 'Hello World'


@app.route('/look', methods=['POST', 'GET'])
def look():
    if request.method == "POST":
        project = request.form.get('project')
        ph_name = select_all_mysql('select distinct ph_name from physics_value_table where project="%s"'%(project))
        da = {}
        for i in ph_name:
            print(i[0])
            daa = {}
            site_num = select_all_mysql(
                'select site,num from physics_value_table where project="%s" and ph_name = "%s"' % (project,i[0]))
            print(site_num)
            # num = select_all_mysql(
            #     'select num from physics_value_table where project="201111" and ph_name = "%s"'%(i[0]))
            # print(num)
            for t in site_num:
                daa[t[0]] = t[1]
                print(t)
            da[i[0]] = daa
            print('--------')

        print(da)

        return da
    else:
        print('sb')

    return render_template('table_01.html')
